package com.tinckay.common.util;

import java.time.*;
import java.time.chrono.ChronoLocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.Date;

/**
 * @Author: Jean
 * @Desc: LocalDateTime日期操作工具类
 * @Date: 2018/10/24 14:57
 */
public class LocalDateTimeUtils {

    public static final DateTimeFormatter DTF_ONE = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DTF_TWO = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DTF_THREE = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");

    /**
     * 字符串转为LocalDateTime
     *
     * @param dateTime
     * @param pattern
     * @return
     */
    public static LocalDateTime parseTime(CharSequence dateTime, DateTimeFormatter pattern) {
        return LocalDateTime.parse(dateTime, pattern);
    }

    /**
     * 字符串转为LocalDate
     *
     * @param dateTime
     * @param pattern
     * @return
     */
    public static LocalDate parseDate(CharSequence dateTime, DateTimeFormatter pattern) {
        return LocalDate.parse(dateTime, pattern);
    }

    public static LocalDate parseDate(CharSequence dateTime) {
        return LocalDate.parse(dateTime, DTF_TWO);
    }

    /**
     * 转换毫秒为LocalDateTime
     *
     * @param millisSeconds
     */
    public static LocalDateTime convertMilliSecondToLDT(long millisSeconds) {
        int nanoOfSecond = 0;
        ZoneOffset zoneOffset = ZoneOffset.ofHours(8);
        return LocalDateTime.ofEpochSecond(millisSeconds / 1000, nanoOfSecond, zoneOffset);
    }

    /**
     * Date转换为LocalDateTime
     *
     * @param date
     */
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    /**
     * LocalDateTime转换为Date
     *
     * @param time
     */
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取指定日期的毫秒
     *
     * @param time
     */
    public static Long getMilliByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    /**
     * 获取指定日期的秒
     *
     * @param time
     */
    public static Long getSecondsByTime(LocalDateTime time) {
        return time.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    /**
     * 获取指定时间的指定格式
     *
     * @param time
     * @param pattern
     */
    public static String formatTime(LocalDateTime time, String pattern) {
        return formatTime(time, DateTimeFormatter.ofPattern(pattern));
    }

    /**
     * 获取指定时间的指定格式
     *
     * @param time
     * @param pattern
     */
    public static String formatTime(LocalDateTime time, DateTimeFormatter pattern) {
        return time.format(pattern);
    }

    /**
     * 开始时间和结束时间校验/合法返回true,不合法返回false
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean checkStartTimeAndEndTime(ChronoLocalDateTime startTime, ChronoLocalDateTime endTime) {
        return endTime.isAfter(startTime);
    }

    /**
     * 获取时间差毫秒数
     */
    public static long betweenMillis(Temporal startTime, Temporal endTime) {
        return Duration.between(startTime, endTime).toMillis();
    }

    /**
     * 获取时间差秒数
     *
     * @param
     */
    public static long betweenSeconds(Temporal startTime, Temporal endTime) {
        return ChronoUnit.SECONDS.between(startTime, endTime);
    }

    public static LocalDate formatDate(String planTime) {
        return LocalDate.parse(planTime, DTF_TWO);
    }


    /**
     * 获取当前日期字符串
     */
    public static String currDate() {
        return LocalDate.now().format(DTF_TWO);
    }

    public static void main(String[] args) {
        System.out.println(currDate());
    }
}

